﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;

namespace Silverlight.Arcgis.MapTip
{
    public partial class 地图放大缩小框选等 : UserControl
    {
        public 地图放大缩小框选等()
        {
            InitializeComponent();

            //初始化变量 
            gra = this.MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            draw = new Draw(this.MyMap)
            {
                LineSymbol = new LineSymbol()
                {
                    Color = new SolidColorBrush(Colors.Red),
                    Width = 2
                },
                FillSymbol = new SimpleFillSymbol()
                {
                    BorderBrush = new SolidColorBrush(Colors.Red) { },
                    BorderThickness = 2,
                    Fill = new SolidColorBrush(Colors.Green) { Opacity = 0.5 }
                },
                DrawMode = DrawMode.Rectangle,
            };
            //注册事件
            draw.DrawComplete += new EventHandler<DrawEventArgs>(draw_DrawComplete);
        }

        /// <summary>
        /// 绘画完成后,显示出来
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void draw_DrawComplete(object sender, DrawEventArgs e)
        {
            switch (CurrentToolType)
            {
                case ToolType.放大:
                    MyMap.ZoomTo(e.Geometry);
                    break;
                case ToolType.缩小:
                    {
                        //根据当前视图范围和绘制的框的比例算出新的视图范围大小
                        Envelope currentExtent = MyMap.Extent;

                        Envelope zoomBoxExtent = e.Geometry as Envelope;
                        MapPoint zoomBoxCenter = zoomBoxExtent.GetCenter();

                        double whRatioCurrent = currentExtent.Width / currentExtent.Height;
                        double whRatioZoomBox = zoomBoxExtent.Width / zoomBoxExtent.Height;

                        Envelope newEnv = null;

                        if (whRatioZoomBox > whRatioCurrent)
                        {
                            double mapWidthPixels = MyMap.Width;
                            double multiplier = currentExtent.Width / zoomBoxExtent.Width;
                            double newWidthMapUnits = currentExtent.Width * multiplier;
                            newEnv = new Envelope(new MapPoint(zoomBoxCenter.X - (newWidthMapUnits / 2), zoomBoxCenter.Y),
                                                           new MapPoint(zoomBoxCenter.X + (newWidthMapUnits / 2), zoomBoxCenter.Y));
                        }
                        else
                        {
                            double mapHeightPixels = MyMap.Height;
                            double multiplier = currentExtent.Height / zoomBoxExtent.Height;
                            double newHeightMapUnits = currentExtent.Height * multiplier;
                            newEnv = new Envelope(new MapPoint(zoomBoxCenter.X, zoomBoxCenter.Y - (newHeightMapUnits / 2)),
                                                           new MapPoint(zoomBoxCenter.X, zoomBoxCenter.Y + (newHeightMapUnits / 2)));
                        }

                        if (newEnv != null)
                            MyMap.ZoomTo(newEnv);
                    }
                    break;
            }
        }

        /// <summary>
        /// 工具按钮按下后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            string modetype = button.Content.ToString();
            //设置绘画的方式
            switch (modetype)
            {
                case "放大":
                    CurrentToolType = ToolType.放大;
                    break;

                case "缩小":
                    CurrentToolType = ToolType.缩小;
                    break;

                case "平移":
                    draw.IsEnabled = false;
                    return;

                case "全图":
                    MyMap.ZoomTo(MyMap.Layers.GetFullExtent());
                    return;
            }
            //激活工具
            draw.IsEnabled = true;
        }

        /// <summary>
        /// 全局变量:绘图用
        /// </summary>
        private Draw draw;
        /// <summary>
        /// 全局变量:绘图层
        /// </summary>
        private GraphicsLayer gra;

        /// <summary>
        /// 声明一个工具枚举
        /// </summary>
        public enum ToolType
        {
            放大,
            缩小,
        }

        /// <summary>
        /// 当前的地图工具
        /// </summary>
        private ToolType CurrentToolType
        {
            get;
            set;
        }

    }
}
